package com.ds.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ds.api.dao.UserWalletMapper;
import com.ds.api.model.UserWallet;
import com.google.gson.Gson;

/**
 * 用户钱包
 * @className: UserWalletController
 * @description: TODO
 * @author xiangwei
 */
@RestController
@RequestMapping("/wallet")
public class UserWalletController extends BaseController {
    
    private static final Logger log = LoggerFactory
            .getLogger(UserWalletController.class);
    @Autowired
    private UserWalletMapper mapper;
    
    @RequestMapping("/update")
    public void wallet(HttpServletRequest request,@RequestParam Map<String,Object> map,HttpServletResponse response){
        String resultStr="0";
        Map<String, Object> result = new HashMap<String, Object>();
        String userId=request.getAttribute("USER_ID")+"";
        map.put("userId", Integer.parseInt(userId));
        List<UserWallet> list=mapper.query(map);
        if(list.size()>0){
            UserWallet wallet=list.get(0);
            if(map.get("cash")!=null){
                Float cash=Float.valueOf(map.get("cash")+"");
                wallet.setCash((cash+Float.valueOf(wallet.getCash()))+"");
            }
            mapper.updateByPrimaryKeySelective(wallet);
        }else{
            UserWallet wallet=new UserWallet();
            if(map.get("cash")!=null){
                wallet.setCash(map.get("cash")+"");
            }
            if(map.get("bond")!=null){
                wallet.setBond(map.get("bond")+"");
            }
            if(map.get("voucher")!=null){
                wallet.setVoucher(map.get("voucher")+"");
            }
            mapper.insert(wallet);
        }
        result.put("result",resultStr);
        Gson gson = new Gson();
        try {
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));
        } catch (Exception e) {
            log.info("程序出错："+e);
        }
    }
}
